﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Communication.Net.Web;
using UtilZ.Dotnet.Ex.Json;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Tool.HostCore
{
    [Route("api/v1/host")]
    public partial class HostBLL
    {
        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        //http://127.0.0.1:5200/api/v1/host/getPluginHomePageInfos
        [HttpGet("getPluginHomePageInfos")]
        public ApiResult GetPluginHomePageInfos()
        {
            try
            {
                string json = JsonSerializer.SerializeObject(this._pluginHomePageInfoList);
                return new ApiResult(ApiResultStatus.Succes, json);
            }
            catch (Exception ex)
            {
                return new ApiResult(ApiResultStatus.Exception, ex.ToString());
            }
        }

        /// <summary>
        /// windows下显示控制台
        /// </summary>
        /// <returns></returns>
        //http://127.0.0.1:5200/api/v1/host/showConsole
        [HttpGet("showConsole")]
        public ApiResult ShowConsole()
        {
            try
            {
                if (EnvironmentEx.IsWindowPlatform())
                {
#pragma warning disable CA1416
                    ConsoleEx.ShowConsole(Console.Title);
#pragma warning restore CA1416
                    return new ApiResult(ApiResultStatus.Succes, $"ok:{TimeEx.GetTimestamp()}");
                }
                else
                {
                    return new ApiResult(ApiResultStatus.Fail, $"本操作只支持windows主机:{TimeEx.GetTimestamp()}");
                }
            }
            catch (Exception ex)
            {
                return new ApiResult(ApiResultStatus.Exception, ex.ToString());
            }
        }

        /// <summary>
        /// windows下隐藏控制台
        /// </summary>
        /// <returns></returns>
        //http://127.0.0.1:5200/api/v1/host/hideConsole
        [HttpGet("hideConsole")]
        public ApiResult HideConsole()
        {
            try
            {
                if (EnvironmentEx.IsWindowPlatform())
                {
#pragma warning disable CA1416
                    ConsoleEx.HideConsole(Console.Title);
#pragma warning restore CA1416
                    return new ApiResult(ApiResultStatus.Succes, $"ok:{TimeEx.GetTimestamp()}");
                }
                else
                {
                    return new ApiResult(ApiResultStatus.Fail, $"本操作只支持windows主机:{TimeEx.GetTimestamp()}");
                }
            }
            catch (Exception ex)
            {
                return new ApiResult(ApiResultStatus.Exception, ex.ToString());
            }
        }


        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        //http://127.0.0.1:5200/api/v1/host/exit
        [HttpGet("exit")]
        public ApiResult Exit()
        {
            try
            {
                //this._protectAppItem.Dispose();

                //HttpClient httpClient = new HttpClient();
                //httpClient.Timeout = TimeSpan.FromSeconds(300);
                //string url = $"http://127.0.0.1:{this._protectAppPort}/api/v1/protect/exit";
                //Task<string> task = httpClient.GetStringAsync(url);
                //var ret = JsonSerializer.DeserializeObject<ApiResult>(task.Result);
                //if (ret.Status != ApiResultStatus.Succes)
                //{
                //    throw new ArgumentException($"退出保护程序失败,{ret.Data}");
                //}

                //开个线程延迟100毫秒再结束,不然结束程序会提示错误信息,可能会误导
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        Thread.Sleep(100);
                        this.Dispose();
                        this._exitCallback();
                    }
                    catch (Exception exi)
                    {
                        Loger.Error(exi);
                    }
                });

                return new ApiResult(ApiResultStatus.Succes, $"ok:{TimeEx.GetTimestamp()}");
            }
            catch (Exception ex)
            {
                return new ApiResult(ApiResultStatus.Exception, ex.ToString());
            }
        }

        //http://192.168.10.96:23018/ctrl/clearConsole
        [HttpGet("clearConsole")]
        public ApiResult HttpClearConsole()
        {
            try
            {
                Console.Clear();
                return new ApiResult(ApiResultStatus.Succes, $"ok:{TimeEx.GetTimestamp()}");
            }
            catch (Exception ex)
            {
                return new ApiResult(ApiResultStatus.Exception, ex.ToString());
            }
        }
    }
}
